home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / educate / wordy442.zip / OFSET.C < prev    next >
C/C++ Source or Header  |  1996-06-20  |  7KB  |  252 lines

  1. /**************************************************************************/
  2. /*                                OFset   Utility                         */
  3. /*                                                                        */
  4. /*                                                                        */
  5. /*                                     M\Cooper                           */
  6. /*                                    PO Box 237                          */
  7. /*                            St. David, AZ 85630-0237                    */
  8. /*                        -------------------------------                 */
  9. /*                        Email:  thegrendel@theriver.com                 */
  10. /*                                                                        */
  11. /*                  $2.00 to register the entire WORDY package            */
  12. /*                                                                        */
  13. /**************************************************************************/
  14.  
  15. #include <conio.h>
  16. #include "srch.h"
  17.  
  18. #define FILE_OPENING_ERROR 3
  19. #define FILENAME_MAXLEN 8
  20. #define CR "\n"
  21. #define MAXLEN 30
  22. #define LINE_LEN 80
  23. #define NOARGS 1
  24. #define INCREMENT 1
  25. #define SPACE ' '
  26. #define WD 8
  27.  
  28. #define BUFFERSIZE 8192
  29.  
  30. char ad[] =
  31. "OFset utility by M\\Cooper, PO Box 237, St. David, AZ 85630-0237";
  32.  
  33. typedef enum { FALSE, TRUE } Boolean;
  34.  
  35. void getword( char *lset, char *filename, int l_number );
  36. void center( char *strng );
  37. Boolean wordtest( char *letterset, char *word, int number_of_letters_sel );
  38.  
  39.  
  40. void main( int argc, char **argv )
  41. {
  42.  
  43.    char letterset [ MAXLEN ],
  44.         filename [ MAXLEN ],
  45.         ans [WD];
  46.         int number;
  47.  
  48.      if( argc == NOARGS )
  49.         {
  50.         clrscr();
  51.         printf( "Enter a LETTERSET to select from ... " );
  52.         gets( letterset );
  53.      strcpy( filename, "word.lst" );
  54.      printf( "\nHow many letters to select from %s? ", letterset );
  55.      gets( ans );
  56.      number = atoi( ans );
  57.         }
  58.      else
  59.      if( argc == NOARGS + 1 )
  60.            {
  61.         strcpy( letterset, *( argv + 1 ) );
  62.         strcpy( filename, "word.lst" );
  63.         clrscr();
  64.         printf( "\nHow many letters to select from %s? ", letterset );
  65.         gets( ans );
  66.         number = atoi( ans );
  67.         }
  68.    else
  69.       if( argc == NOARGS + 2 )
  70.         {
  71.         strcpy( letterset, *( argv + 1 ) );
  72.         number = atoi( *( argv + 2 ) );
  73.         strcpy( filename, "word.lst" );
  74.         }
  75.  
  76.    else
  77.      {
  78.      strcpy( letterset, *( argv + 1 ) );
  79.      number = atoi( *( argv + 2 ) );
  80.      strcpy( filename,  *( argv + 3 ) );
  81.      }
  82.  
  83.      getword( letterset, filename, number );
  84. }
  85.  
  86.  
  87. /**********************************WORDTEST********************************/
  88. /*       Function tests if word is constructible from Letterset           */
  89. /*                 Args in: char *letterset, char *word                   */
  90. /*   Returns: error_flag == TRUE (1) if constructible, FALSE (0) if not   */
  91. /**************************************************************************/
  92.  
  93. Boolean wordtest( char *letterset, char *word, int lnumber )
  94. {
  95.    int hits = 0;
  96.  
  97.       while( *letterset )
  98.          {
  99.          if( strchr( word, *letterset ) )
  100.             hits++;
  101.  
  102.          letterset++;
  103.          }
  104.       
  105.       if( hits >= lnumber )
  106.          return( TRUE );
  107.       else
  108.          return( FALSE );
  109. }
  110.  
  111. /*************************************************************/
  112. void getword( char *letter_set, char *filename, int number )
  113. {
  114.  
  115.     char    l_set [ MAXLEN ],
  116.         word [ MAXLEN ],
  117.         tempstr [ LINE_LEN + 1 ],
  118.         targetfile [ MAXLEN ],
  119.         bar [ LINE_LEN + 1 ],
  120.    wd [ WD ],
  121.    ltr [ WD ],
  122.         double_bar [ LINE_LEN + 1 ],
  123.   file_suffix[] = ".ofs";
  124.  
  125.     FILE *fptr,
  126.         *tfile;
  127.     int fnamelen;
  128.     long wcount = 0L;
  129.  
  130.        memset( bar, '-', LINE_LEN );
  131.        *( bar + LINE_LEN ) = NULL;
  132.        memset( double_bar, '=', LINE_LEN );
  133.        *( double_bar + LINE_LEN ) = NULL;
  134.  
  135.       strcpy( l_set, letter_set);
  136.  
  137.        /*************opening credits*****************************/
  138.        clrscr();
  139.        printf( double_bar );
  140.        strcpy( tempstr, ad );
  141.        center ( tempstr );
  142.        printf( tempstr );
  143.        printf( CR );
  144.        printf( double_bar );
  145.        printf( CR );
  146.        /*********************************************************/
  147.        /*   Create name of file to store derived words in       */
  148.        /*********************************************************/
  149.        fnamelen = strlen( l_set );
  150.        if( fnamelen  > FILENAME_MAXLEN )
  151.           fnamelen = FILENAME_MAXLEN;
  152.        strncpy( targetfile, l_set, fnamelen );
  153.        *( targetfile + fnamelen ) = NULL;
  154.        //NULL-terminate string, so strcat works, ha, ha.
  155.        strcat( targetfile, file_suffix );
  156.       /*********************************************************/
  157.  
  158.        if( !( fptr = fopen( filename, "rt" ) ) )
  159.          {
  160.          printf( "\7\7\7Cannot open Wordfile!" );
  161.          exit( FILE_OPENING_ERROR );
  162.          }
  163.       if( setvbuf( fptr, NULL, _IOFBF, BUFFERSIZE ) )
  164.          exit( FILE_OPENING_ERROR + 1 );
  165.  
  166.        if( !( tfile = fopen( targetfile, "wt" ) ) )
  167.          {
  168.          printf( "\7\7\7Cannot open file to save words in!" );
  169.          exit ( FILE_OPENING_ERROR + 2 );
  170.          }
  171.       if( setvbuf( tfile, NULL, _IOFBF, BUFFERSIZE ) )
  172.          exit( FILE_OPENING_ERROR + 3 );
  173.  
  174.        /**************'Wait' Message************/
  175.        printf( CR CR );
  176.        printf( "WORKING...\n\n" );
  177.        printf( "This will take a few seconds...\n" );
  178.        printf( "Please be patient.\n\n" );
  179.        printf( "Now searching 100,000+ word file \nand writing file of valid words containing at least %d letter(s) of -%s-.\n\n", 
  180.             number, letter_set );
  181.        /*****************************************/
  182.  
  183.  
  184.        if( number == INCREMENT )
  185.            strcpy( ltr, "letter" );
  186.        else
  187.            strcpy( ltr, "letters" );
  188.  
  189.  
  190.        sprintf( tempstr, "Word(s) selected with at least %d %s from: %s.\n",
  191.             number, ltr, strupr( l_set ) );
  192.        center( tempstr );
  193.        fprintf( tfile, double_bar );
  194.       fprintf( tfile, CR );
  195.        fprintf( tfile, tempstr );
  196.        fprintf( tfile, double_bar );
  197.        fprintf( tfile, CR );
  198.  
  199.  
  200.          /*********************Main Loop*************/     
  201.           while( fgets( word, MAXLEN, fptr ) != NULL )
  202.  
  203.             if( wordtest( letter_set, word, number ) )
  204.                {
  205.                fprintf( tfile, "%s", word );
  206.                wcount++;
  207.                }
  208.           /*******************************************/
  209.  
  210.       if( wcount == INCREMENT )
  211.           strcpy( wd, "word" );
  212.       else 
  213.           strcpy( wd, "words" );
  214.  
  215.           fprintf( tfile, bar );
  216.       fprintf( tfile, CR );
  217.           sprintf( tempstr, "%ld %s can be constructed using at least %d %s from %s.",
  218.                  wcount, wd, number, ltr, l_set );
  219.           center( tempstr );              
  220.           fprintf( tfile, tempstr );
  221.           fprintf( tfile, "\n\n" );
  222.  
  223.           center( ad );
  224.           fprintf( tfile, ad );
  225.  
  226.           fcloseall();
  227.  
  228.           sprintf( tempstr,
  229.                  "The file %s has %ld %s containing at least %d %s of %s\7.\n\n",
  230.                  targetfile, wcount, wd, number, ltr, l_set );
  231.           center( tempstr );
  232.           printf( CR CR );
  233.           printf( tempstr );
  234.  
  235. }
  236.  
  237.  
  238.  
  239. void center( char *str )
  240. {
  241.    int padding;
  242.    char st [ LINE_LEN + INCREMENT ];
  243.  
  244.      padding = LINE_LEN / 2 - strlen( str ) / 2;
  245.      memset( st, SPACE, padding );
  246.      *( st + padding ) = NULL;  //Terminate string
  247.      strcat( st, str );
  248.      strcpy( str, st );
  249.  
  250.      return;
  251. }
  252.